package net.siufung.market.base.tenant.controller.regulation;

import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.tenant.mapper.entity.TenantMenu;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 陈建峰
 * @since 2022/6/28 2:53 下午
 */
@AllArgsConstructor
public class TenantMenuCreateBatchRegulation extends AbstractRegulation<List<TenantMenu>> {

    private static final long serialVersionUID = -3940610362647027207L;

    @Override
    public boolean isSatisfiedBy(List<TenantMenu> tenantMenuList) {
        if(CollectionUtils.isEmpty(tenantMenuList)){
            throw new BizServiceException("菜单列表不能为空！");
        }
        if(!checkNotRepeatMenuId(tenantMenuList)){
            throw new BizServiceException("参数中菜单Id重复！");
        };
        return Boolean.TRUE;
    }

    /**
     * 判断不重复菜单Id
     * @param tenantMenuList 租户菜单Id集合
     */
    private Boolean checkNotRepeatMenuId(List<TenantMenu> tenantMenuList){
        return tenantMenuList.size() == tenantMenuList.stream()
                .map(TenantMenu::getMenuId).count();
    }
}
